home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 288_01.zip / KNAUSS.TXT < prev    next >
Text File  |  1993-04-01  |  14KB  |  239 lines

  1. A Poor Man's Solution to the Traveling Salesman Problem
  2.  
  3. Kevin E Knauss
  4.  
  5. 12 Mar 89
  6.  
  7. Given a map and a means of transportation, a competent traveling
  8. salesman can pick a reasonable route to all his customers. The route he
  9. picks needn't be optimal just practical. In this article, we'll explore
  10. a hypothetical salesman's intuitive approach to finding a practical
  11. route to all his customers and its implementation in the C programming
  12. language. In an attempt to find elegant optimal solutions to tough
  13. problems, we often overlook solutions which appear to be rather
  14. "brutish." Upon closer inspection, however, we see that these solutions
  15. are more elegant than they appear and, better yet, they work!
  16.  
  17. BACKGROUND
  18.  
  19. Routing and scheduling problems are inherently difficult to solve
  20. because they often require total enumeration of all possible outcomes. 
  21. As the number of data points in the problem increases, the possible
  22. outcomes increase exponentially. With the Traveling Salesman Problem
  23. (TSP) for instance, studies have shown that an algorithm which yields an
  24. exact solution is relatively infeasible for networks containing in
  25. excess of 100 points. In fact, there are problems with as few as 48
  26. cities for which the best answer found has not been proven to be
  27. optimal. By using heuristics of various types, one is enabled to find
  28. feasible (though not necessarily optimal) solutions to these otherwise
  29. "unsolvable" problems. 
  30.  
  31. The TSP simply stated is: "A salesman, starting in one city, wishes to
  32. visit each n─1 other cities once and only once and return to the start. 
  33. In what order should he visit the cities to minimize the total distance
  34. traveled?" (8) This may seem too trivial a task to have generated active
  35. research for over three decades (the literature I've researched goes
  36. back to 1958), but there are many practical applications for the
  37. solution of this basic problem. Automated drilling machines and the
  38. newer laser drills used to drill printed circuit boards, for example,
  39. may have hundreds or thousands of holes to drill and often spend as much
  40. time traveling to a position for a hole as they do drilling it. 
  41. Programming efficient head travel for these machines could make the
  42. difference for a company to turn a profit or loss. Solve the more basic
  43. TSP, and the marginal printed circuit board company may be able to stay
  44. in business by applying the same techniques. 
  45.  
  46. The TSP is one which is, to quote an old cliche, "easier said than
  47. done." That is, it is easy to explain the problem but difficult to
  48. solve; at least it seems difficult to solve when we look at many of the
  49. purely mathematical models that are, too often, not related back to the
  50. original problem. I chose to attack the TSP in a simplistic manner in
  51. an attempt to find one or more algorithms which would approximate a
  52. person's intuitive approach. In so doing, I was able to find an
  53. efficient way to "solve" the problem by producing acceptable results to
  54. large─scale traveling salesman problems. (Note that the word
  55. "acceptable" implies that judgments are required.)
  56.  
  57. TERMINOLOGY
  58.  
  59. Before we begin traveling around, a review of TSP terminology is in
  60. order. We'll begin with the city, our most basic term. This is what
  61. will be visited and may also be referred to as a town, point, node, or
  62. vertex. One goes from one city to the next by traveling a given
  63. distance or incurring a specified cost. The terms link, arc, and edge
  64. are also used in place of distance or cost. The collection of all the
  65. cities and the distances between each pair is a network or graph and is
  66. often represented by a distance matrix. A salesman will follow a route
  67. to visit each of the cities in the network, and this route may also be
  68. called a tour, path, or circuit. Finally, if we remove two or more
  69. links from the completed tour, we will break it into sub─paths or chains
  70. of cities. 
  71.  
  72. The distance matrix is a two dimensional array where the horizontal or
  73. row vector (dimension) is identical to the vertical or column vector. 
  74. The cell found at each intersection contains the distance or cost
  75. between the city represented by the horizontal coordinate and the city
  76. represented by the vertical coordinate. Those familiar with graph
  77. theory haven't seen anything new here. If you've seen a lot of these
  78. terms for the first time, however, don't be afraid to refer back, for
  79. I'll be using many of them interchangeably. 
  80.  
  81. APPROACH
  82.  
  83. Let's now consider the problem in terms of a salesman who must visit a
  84. dozen or so cities in the state of Hypothetica. Since the salesman must
  85. leave and return to the same city and visit all other cities in the
  86. process, his tour will be some sort of loop through the state. 
  87. Obviously, as a loop is unbroken, one may start at any point on the the
  88. tour and still trace the same loop. Thus the starting city is of no
  89. consequence; rather we want to find the best route irregardless of the
  90. salesman's starting point. 
  91.  
  92. Intuitively, one would want to travel to cities nearby and to cities
  93. near those. We can build a procedure based on this thought by first
  94. finding the closest two cities and then continuing to the next closest
  95. city that hasn't been visited. This should produce a fairly good tour,
  96. or at least would seem so at first. It may turn out that this tour
  97. isn't optimal, but it's a reasonable solution for starters. 
  98.  
  99. As the cities are exhausted from our network, we have fewer choices to
  100. make. Intuitively, we may reason that the choices left to us may not be
  101. as good as those we're offered in the early stages of tour building. 
  102. Our salesman may be forced to backtrack and cross previously traversed
  103. arcs. If we check the proximity of neighboring cities, however,
  104. especially those near the end of the initial tour, we may be able to
  105. find improvements. 
  106.  
  107. One approach we may try involves the removal of a single city from the
  108. tour and testing it between each pair of cities in the remaining tour. 
  109. Once we've tested it in each location, we'll place it in the location
  110. where the overall circuit cost is lowest (i.e. the shortest distance
  111. the salesman must travel). This same approach may be tried with chains
  112. of cities of varying lengths, but with chains we must also check for
  113. orientation (that is try the chain both frontward and backward between
  114. each pair of cities). This leaves us with our last thought of simply
  115. checking the orientation of a chain in its original location. If you
  116. think a picture is worth a thousand words, see Figure 1 so we can cut
  117. 4,000 words from this article. By testing the proximity of every city
  118. or the proximity and orientation of every chain, we can be fairly
  119. confident that any ill effects produced by our original technique will
  120. be cleaned up. 
  121.  
  122. If we look through related literature, we find that our tour building
  123. and improvement techniques have already been studied and named. Our
  124. tour building algorithm is known as the nearest neighbor or greedy
  125. algorithm. Our tour improvement algorithms generally fall into a
  126. category known as k─optimality or k─opt for short. A tour is said to be
  127. k─optimal if we are unable to improve it by removing any k arcs and
  128. replacing them with k others. Checking chain orientation in place is
  129. the same as removing two arcs and replacing them with two others and is
  130. thus the 2─opt algorithm. Likewise, chain proximity and orientation is
  131. the 3─opt algorithm with point proximity a special case where the chain
  132. to be tested has length one. Even though these improvement techniques
  133. are related, we'll evaluate each on its own merits. 
  134.  
  135. IMPLEMENTATION
  136.  
  137. To evaluate the intuitive approach we may embark upon an elaborate
  138. mathematical analysis that may or may not produce any conclusive
  139. results, or we may implement the solutions in practical models that may
  140. be run against live data. If this was a scientific journal, we'd follow
  141. the mathematical tack; but since this is a practical journal, we'll try
  142. the modeling approach. 
  143.  
  144. The main functions are programmed in their own modules called:
  145. NearNeighbor, PointOpt, TwoOpt, Hybrid, and ThreeOpt (listings 1 through
  146. 5 respectively). NearNeighbor generates the initial tour from the
  147. distance matrix while the other routines take turns improving it. 
  148. PointOpt performs point proximity improvement only, and TwoOpt performs
  149. only chain orientation improvement. Hybrid combines point proximity and
  150. chain orientation improvements while ThreeOpt adds chain proximity and
  151. orientation. The nearest neighbor, 2─Opt, and 3─Opt algorithms have
  152. been studied in detail within the field, but are normally regarded as
  153. independent techniques. To my knowledge, this is the first that point
  154. proximity has been considered either independently (PointOpt) or in
  155. conjunction with the 2─Opt algorithm (Hybrid). 
  156.  
  157. We'll use six distance matrices found in the literature to test our
  158. procedures since these networks have known optima (or at least a best
  159. known solution as is the case of the 48 city problem). We'll need to
  160. know the tour length each procedure produces and the time it takes to
  161. find the tour. We can calculate from this information how much
  162. improvement is made by each technique and what percentage each solution
  163. is from the known optimum. To see how the improvement techniques work
  164. on different initial tours, we'll reverse the initial nearest neighbor
  165. tour and generate a bad initial tour. 
  166.  
  167. To capture the time, we'll need a system dependent routine. GetTime
  168. samples the clock counter by issuing an interrupt under MS─DOS;
  169. ElapsedTime calls GetTime and compares the new time with a previous time
  170. passed in. Listing 6 shows GetTime implemented using the MIX C compiler
  171. for MS─DOS and ElapsedTime in a plain vanilla implementation. For the
  172. bad initial tour, we'll simply reverse the logic of the nearest neighbor
  173. algorithm to generate a farthest neighbor tour (listing 7). The driver
  174. program (listing 8) is far from elegant but gets the job done. 
  175.  
  176. OBSERVATIONS
  177.  
  178. One might assume that, since it embodies all the techniques used in the
  179. other improvement algorithms, the 3─Opt algorithm would produce a tour
  180. at least as good as the others. Our results show that one might be
  181. wrong in such an assumption! In fact, the 3─Opt algorithm only found the
  182. best solution in the two smallest problems and other algorithms found
  183. the same solutions (see Figure 2). The total time to run the three tour
  184. building routines and the three lesser improvement routines on each is
  185. less than the time needed to run the 3─Opt routine just once on one of
  186. the larger problems (see Figure 3). This indicates that the 3─Opt
  187. algorithm may not be very valuable as a tour improvement algorithm. 
  188.  
  189. The independent point proximity algorithm is likewise not very valuable. 
  190. It was 15% faster than the Hybrid routine overall but fell well behind
  191. in performance. PointOpt found the best solution in the 10 city
  192. problem, but Hybrid found the same solution. Hybrid outperformed
  193. PointOpt in all the other solutions so nothing is gained by running both
  194. routines. 
  195.  
  196. We can see that the TwoOpt and Hybrid routines compliment each other
  197. very nicely. Where Hybrid didn't find the best solution, TwoOpt did. 
  198. The 2─Opt algorithm was consistently the fastest and the point proximity
  199. 2─Opt hybrid found the best answer in four of the six problems. Their
  200. combined times plus the time to build the initial tour was comparable to
  201. the time required to read the distance matrix on my 12Mhz AT clone with
  202. 28ms access hard drive. 
  203.  
  204. One thing that may be a surprise, is that our tour improvement
  205. algorithms are dependent upon how compatible the initial tour is with
  206. the techniques being applied and not necessarily how close to optimum
  207. that tour is. In all but one problem, the best improvement was found
  208. when starting from the nearest neighbor solution. In the 42 city
  209. problem, however, the best improvement was found by starting with the
  210. farthest neighbor solution. In addition to being found from the nearest
  211. neighbor solution, the same improvement was found in less time from the
  212. farthest neighbor solution in the 10 and 20 city problems. 
  213.  
  214. The MIX C compiler/linker with its .COM executable files causes some
  215. problems for large scale applications such as the drilling machines. I
  216. had no problem with our small to medium scale problems, but when I
  217. compiled the program with dimensions over 100 the program ran out of
  218. space. Dynamic storage allocation won't cure the problem since the heap
  219. space for dynamic storage allocation and the stack space for static data
  220. structures all come from the same pot. Answers will have to come from a
  221. C environment that allows for .EXE executable files or the use of disk
  222. resident dynamic arrays. The latter of these will degrade execution
  223. time, but taking a long time to find a solution is better than taking a
  224. short time to not find one at all! Note that by using the function
  225. ArcCost to access distance matrix data we've made it easy to try
  226. differing storage methods. 
  227.  
  228. CONCLUSION
  229.  
  230. Following an intuitive approach to a problem and implementing that
  231. approach can often produce very acceptable results. Though there can be
  232. no substitute for thorough analysis, neither can there be a substitute
  233. for experimentation and testing of hypotheses. The modularity of C with
  234. its procedures and functions allows the building blocks of
  235. experimentation to become the building blocks of application. With our
  236. TSP modeling, we need only develop a new driver program to build an
  237. efficient production package. 
  238.  
  239.